//
//  HSYSelectCityViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/6.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "HSYSelectCityViewController.h"
#import "UIImage+Tools.h"
#import "HSYGetCityRequest.h"
#import "BQActivityView.h"
#import "HSYCityModel.h"
#import "HSYSelectProvinceViewController.h"

@interface HSYSelectCityViewController ()

@property(nonatomic,strong)NSArray *dataList; //数据源

@end

@implementation HSYSelectCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = YES;
    self.tableView.tableFooterView = [UIView new];
    
    [self.leftAreaLBtn setImage:[UIImage imageNamed:@"left"]  forState:UIControlStateNormal];
    [self.titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    if (self.parentModel) {
        NSString *title = @"选择市";
        if ([self.parentModel.atype integerValue] == 1) {
            title = @"选择市";
        }
        if ([self.parentModel.atype integerValue] == 2) {
            title = @"选择县";
        }
        [self.titleButton setTitle:title forState:UIControlStateNormal];
    }
    
    [self getList];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupNavigationBarBackgroundImage{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(1, 1)]  forBarMetrics:UIBarMetricsDefault];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}

- (BOOL)hsy_allowPullUp{
    return NO;
}

- (BOOL)hsy_allowPullDown{
    return NO;
}

-(void)getChildrenWithModel:(HSYCityModel *)cityModel callback:(void(^)(NSString *title,NSString *code))info{
    self.parentModel = cityModel;
    self.selectedCityBlock = info;
}

- (void)getList{
    
    HSYGetCityRequest *task = [[HSYGetCityRequest alloc] init];
    task.parentCode = self.parentModel.code;
    
    __weak typeof(self)weakSelf = self;
    [BQActivityView showActiviTy];
    
    [HSYNetworkManager POST:task success:^(HSYResponse *response) {
        [BQActivityView hideActiviTy];
        
        if (response.resultCode == HSY_REQUEST_CODE_SUCCESS) {
            weakSelf.dataList = [HSYCityModel mj_objectArrayWithKeyValuesArray:response.encryptDataResult];
            [weakSelf.tableView reloadData];
        }else{
            [weakSelf showMessage:response.resultMessage];
        }
    } failure:^(NSError *error) {
        [BQActivityView hideActiviTy];
        
        if (error.code != NSURLErrorCancelled) {
            [weakSelf showMessage:@"请求超时"];
        }
    }];
}

#pragma mark UITableViewDelegate,UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataList.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell2"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell2"];
    }
    
    HSYCityModel *model = self.dataList[indexPath.row];
    
    if (!model.isLeaf && (([self.parentModel.atype integerValue]+1) < self.leafCode || self.leafCode == 0)) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    
    cell.textLabel.text = model.aname;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    HSYCityModel *model = self.dataList[indexPath.row];
    
    if (model.isLeaf || (([self.parentModel.atype integerValue]+1) >= self.leafCode && self.leafCode != 0)) {
        if (self.selectedCityBlock) {
            self.selectedCityBlock([NSString stringWithFormat:@"%@/%@",self.parentModel.aname ,model.aname],model.code);
        }
        
        //查找要推到那个界面
        
        NSInteger i = 0;
        for (i = 0; i < self.navigationController.viewControllers.count; i++) {
            UIViewController *vc = self.navigationController.viewControllers[i];
            if ([vc isKindOfClass:[HSYSelectProvinceViewController class]]) break;
        }
        
        if (i > 0) i--;
        
        [self.navigationController popToViewController:self.navigationController.childViewControllers[i] animated:YES];
    }else{
        
        __weak typeof(self)weakSelf = self;

        HSYSelectCityViewController *vc = [[HSYSelectCityViewController alloc] init];
        vc.leafCode = self.leafCode;
        [vc getChildrenWithModel:model callback:^(NSString *title, NSString *code) {
            if (weakSelf.selectedCityBlock) {
                weakSelf.selectedCityBlock(title, code);
            }
        }];
        
        [self.navigationController pushViewController:vc animated:YES];
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
